package com.imooc.sell.controller;

import com.imooc.sell.VO.ResultVo;
import com.imooc.sell.converter.OrderForm2OrderDTOConverter;
import com.imooc.sell.dataobject.OrderDetail;
import com.imooc.sell.dataobject.OrderMaster;
import com.imooc.sell.dto.OrderDTO;
import com.imooc.sell.enums.ResultEnum;
import com.imooc.sell.exception.SellException;
import com.imooc.sell.form.OrderForm;
import com.imooc.sell.service.OrderService;
import com.imooc.sell.utils.ResultVoUtil;
import lombok.extern.log4j.Log4j;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.criterion.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import javax.xml.transform.Result;
import java.awt.print.Pageable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/buyer/order")
@Slf4j
@Validated
public class BuyerOrderController {

  @Autowired
  private OrderService orderService;

  //创建订单
  @PostMapping(value = "/create")
  public ResultVo<Map<String, String>> create(@Valid OrderForm orderForm, BindingResult bindingResult) {
    if (bindingResult.hasErrors()) {
      log.error("【创建订单】参数不正确，orderForm={}", orderForm);
      throw new SellException(ResultEnum.PARAMS_ERROR.getCode(), bindingResult.getFieldError().getDefaultMessage());
    }
    //转换获取到的对象
    OrderDTO orderDTO = OrderForm2OrderDTOConverter.convert(orderForm);
    if (CollectionUtils.isEmpty(orderDTO.getOrderDetailList())) {
      log.error("【创建订单】,购物车信息为空", orderDTO.getOrderDetailList());
      throw new SellException(ResultEnum.CART_EMPTY);
    }

    OrderDTO createResult = orderService.create(orderDTO);
    Map<String, String> map = new HashMap<String, String>();
    map.put("orderId", createResult.getOrderId());
    return ResultVoUtil.success(map);
  }

  //查询所有订单
  @GetMapping(value = "/listAll")
  public ResultVo<Pageable> listAll(@RequestParam(value = "page", defaultValue = "0") Integer page,
                                    @RequestParam(value = "zise", defaultValue = "10") Integer size) {
    PageRequest pageRequest = PageRequest.of(page, size, Sort.by("createTime").descending());
    Page<OrderMaster> orderMasterPage = orderService.findAll(pageRequest);
    HashMap map = new HashMap();
    map.put("pages", orderMasterPage.getTotalPages());
    map.put("result", orderMasterPage.getContent());
    map.put("total", orderMasterPage.getTotalElements());
    map.put("currentPage", orderMasterPage.getNumber());
    return ResultVoUtil.success(map);
  }

  //查询单个订单
  @GetMapping(value = "/findOne")
  public ResultVo<OrderDTO> findOne(@NotNull(message = "id不能为空") String id) {
    if (StringUtils.isEmpty(id)) {
      log.error("【查询单个订单】 id不能为空");
      throw new SellException(ResultEnum.ORDER_ID_NOT_NULL);
    }
    OrderDTO orderDTO = orderService.findOne(id);
    return ResultVoUtil.success(orderDTO);
  }

  //查询某个用户的订单列表
  @GetMapping(value = "/list")
  public ResultVo<List<OrderDTO>> list(@NotNull(message = "openid不能为空") String openid,
                                       @RequestParam(value = "page", defaultValue = "0") Integer page,
                                       @RequestParam(value = "size", defaultValue = "10") Integer size) {
    PageRequest pageRequest = PageRequest.of(page, size);
    Page<OrderDTO> orderDTOPage = orderService.findList(openid, pageRequest);
    return ResultVoUtil.success(orderDTOPage);
  }

  //取消订单
  @PostMapping(value = "/cancel")
  public ResultVo<OrderDTO> cancel(@NotNull(message = "orderId不能为空") String orderId) {
    OrderDTO orderDTO = new OrderDTO();
    orderDTO = orderService.findOne(orderId);
    OrderDTO result = orderService.cancel(orderDTO);
    return ResultVoUtil.success(result);
  }

  //完结订单
  @PostMapping(value = "/finish")
  public ResultVo<OrderDTO> finish(@NotNull(message = "orderId不能为空") String orderId) {
    OrderDTO orderDTO = new OrderDTO();
    orderDTO = orderService.findOne(orderId);
    OrderDTO result = orderService.finish(orderDTO);
    return ResultVoUtil.success();
  }

  //支付订单
  @PostMapping(value = "/pay")
  public ResultVo<OrderDTO> pay(@NotNull(message = "orderId不能为空") String orderId) {
    OrderDTO orderDTO = new OrderDTO();
    orderDTO = orderService.findOne(orderId);
    OrderDTO result = orderService.pay(orderDTO);
    return ResultVoUtil.success();
  }
}
